<template>
  <el-card style="width: 500px;padding: 20px;">
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <div style="text-align: center;margin: 10px 0">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9090/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <!--      请求要写http的格式，https会报错，需要特殊处理   -->
          <!--          用户头像所展示的标签   -->
          <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <el-form-item label="用户名" prop="username">
        <el-input size="medium" style="margin: 10px 0" disabled placeholder="用户名" prefix-icon="el-icon-user"
                  v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input size="medium" style="margin: 10px 0" placeholder="用户名" prefix-icon="el-icon-user"
                  v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input size="medium" style="margin: 10px 0" placeholder="用户名" prefix-icon="el-icon-user"
                  v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input size="medium" style="margin: 10px 0" placeholder="用户名" prefix-icon="el-icon-user"
                  v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input size="medium" style="margin: 10px 0" placeholder="用户名" prefix-icon="el-icon-user"
                  v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submitForm">提交</el-button>
        <el-button type="warning" size="small" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import userApi from "@/utils/Api/UserApi";

export default {
  name: "Persion",
  data() {
    return {
      form: {
        id: '',
        username: '',
        password: '',
        avatarUrl: '',
        nickname: '',
        email: '',
        phone: '',
        address: ''
      },
      rules: {
        /*
          * 验证规则,失去焦点进行校验，并且在输入的时候也进行校验
          * required: true, message: '请输入用户名', trigger: 'blur',
          * trigger: 'change'
          * */
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {trigger: 'change'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {trigger: 'change'}
        ],
        avatarUrl: [
          {required: true, message: '请输入头像', trigger: 'blur'},
          {trigger: 'change'}
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {trigger: 'change'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {trigger: 'change'}
        ],
        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
          {trigger: 'change'}
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'},
          {trigger: 'change'}
        ]
      },
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  methods: {
    resetForm() {
      // 若所有数据都是表单中的数据，则可以直接调用表单自带的resetFields方法，清除表单中的数据
      // this.$refs.ruleForm.resetFields();
      // 除去用户名其他字段重置
      this.form.nickname = ''
      this.form.email = ''
      this.form.phone = ''
      this.form.address = ''
    },
    // 上传文件，发送给后端请求，成功响应后的钩子函数
    handleAvatarSuccess(response, file, fileList) {
      this.form.avatarUrl = response
    },
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 提交表单
          userApi.updateUserInfo(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            }
            this.$emit("updateUserInfo")
          })
        } else {
          this.$message({
            message: '修改失败',
            type: 'error'
          })
        }
      })
    }
  },
  mounted() {
    // 获取当前用户信息
    const params = {
      username: this.user.username
    }
    userApi.getUserInfo(params).then(res => {
      if (res.code === 200) {
        this.form = res.data
      }
    })
  }
}

</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px solid #333333;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>